<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="google-site-verification" content="fjDZF2ivELjrZI5niPw1zuWUx-5Ta0Iy9unh0KSTioM">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.1.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="前言毕业一年多了，过去一段时间从事的是前端开发的工作，如果别人问我为什么是会从事这个行业，其实我也不知道，阴差阳错吧，毕竟大学也没有学习前端开发的。现在开始接触自己专业的东西（web端的gis），这样既可以利用之前的工作经验，还可以让自己大学期间所学的东西能够派得上用场。话不多说，下面给大家简单说下openlayer在vue中的简单运用。 vue项目搭建项目的搭建需要安装node，打包工具和vue">
<meta name="keywords" content="js,openlayer,gis">
<meta property="og:type" content="article">
<meta property="og:title" content="OpenLayer在vue中的简单使用">
<meta property="og:url" content="https://sean1224.github.io/2019/05/19/OpenLayer在vue中的简单使用/index.html">
<meta property="og:site_name" content="時光深巷夜叶猫">
<meta property="og:description" content="前言毕业一年多了，过去一段时间从事的是前端开发的工作，如果别人问我为什么是会从事这个行业，其实我也不知道，阴差阳错吧，毕竟大学也没有学习前端开发的。现在开始接触自己专业的东西（web端的gis），这样既可以利用之前的工作经验，还可以让自己大学期间所学的东西能够派得上用场。话不多说，下面给大家简单说下openlayer在vue中的简单运用。 vue项目搭建项目的搭建需要安装node，打包工具和vue">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-05-19T10:58:56.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="OpenLayer在vue中的简单使用">
<meta name="twitter:description" content="前言毕业一年多了，过去一段时间从事的是前端开发的工作，如果别人问我为什么是会从事这个行业，其实我也不知道，阴差阳错吧，毕竟大学也没有学习前端开发的。现在开始接触自己专业的东西（web端的gis），这样既可以利用之前的工作经验，还可以让自己大学期间所学的东西能够派得上用场。话不多说，下面给大家简单说下openlayer在vue中的简单运用。 vue项目搭建项目的搭建需要安装node，打包工具和vue">





  
  
  <link rel="canonical" href="https://sean1224.github.io/2019/05/19/OpenLayer在vue中的简单使用/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>OpenLayer在vue中的简单使用 | 時光深巷夜叶猫</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">時光深巷夜叶猫</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://sean1224.github.io/2019/05/19/OpenLayer在vue中的简单使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="li jian">
      <meta itemprop="description" content="出走半生，归来仍是少年">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="時光深巷夜叶猫">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">OpenLayer在vue中的简单使用

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-05-19 18:53:50 / 修改时间：18:58:56" itemprop="dateCreated datePublished" datetime="2019-05-19T18:53:50+08:00">2019-05-19</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/OpenLayer/" itemprop="url" rel="index"><span itemprop="name">OpenLayer</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/2019/05/19/OpenLayer在vue中的简单使用/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/05/19/OpenLayer在vue中的简单使用/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>毕业一年多了，过去一段时间从事的是前端开发的工作，如果别人问我为什么是会从事这个行业，其实我也不知道，阴差阳错吧，毕竟大学也没有学习前端开发的。现在开始接触自己专业的东西（web端的gis），这样既可以利用之前的工作经验，还可以让自己大学期间所学的东西能够派得上用场。话不多说，下面给大家简单说下openlayer在vue中的<strong>简单运用</strong>。</p>
<h2 id="vue项目搭建"><a href="#vue项目搭建" class="headerlink" title="vue项目搭建"></a>vue项目搭建</h2><p>项目的搭建需要安装node，打包工具和vue-cli<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">npm install webpack --save</span><br><span class="line">npm install vue-cli --save</span><br></pre></td></tr></table></figure></p>
<p>接下来进入项目目录，输入以下命令后，会有对话框提示输入一些信息，默认可直接回车。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue init webpack project-name</span><br></pre></td></tr></table></figure></p>
<p>通过生成的package.json文件夹生成依赖包<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install</span><br></pre></td></tr></table></figure></p>
<p>启动服务器<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run dev</span><br></pre></td></tr></table></figure></p>
<h2 id="openlayer的简单实例"><a href="#openlayer的简单实例" class="headerlink" title="openlayer的简单实例"></a>openlayer的简单实例</h2><h4 id="第一个实例"><a href="#第一个实例" class="headerlink" title="第一个实例"></a>第一个实例</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt; </span><br><span class="line">  &lt;div id=&quot;map&quot;&gt;&lt;/div&gt;</span><br><span class="line">&lt;/template&gt; </span><br><span class="line">&lt;script&gt;</span><br><span class="line">import &apos;ol/ol.css&apos;;</span><br><span class="line">import &#123;Map, View&#125; from &apos;ol&apos;;</span><br><span class="line">import TileLayer from &apos;ol/layer/Tile&apos;;</span><br><span class="line">import OSM from &apos;ol/source/OSM&apos;;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &apos;&apos;,</span><br><span class="line">  data () &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      msg: &apos;Welcome to Ol&apos;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  mounted () &#123;</span><br><span class="line">    //最重要的地图对象</span><br><span class="line">    const map = new Map(&#123;</span><br><span class="line">    //让id为map 的div做为地图的容器</span><br><span class="line">    target: &apos;map&apos;,</span><br><span class="line">    //添加地图图层，可添加多个，这里添加的是open street map的瓦片图层</span><br><span class="line">    layers: [</span><br><span class="line">      new TileLayer(&#123;</span><br><span class="line">        source: new OSM()</span><br><span class="line">      &#125;)</span><br><span class="line">    ],</span><br><span class="line">    //定义视图，只能有一个</span><br><span class="line">    view: new View(&#123;</span><br><span class="line">      center: [0, 0],//定义地图显示中心为经度为0，纬度也为0</span><br><span class="line">      zoom: 0//定义地图的显示层级为0</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;!-- Add &quot;scoped&quot; attribute to limit CSS to this component only --&gt;</span><br><span class="line">&lt;style scoped lang=&apos;scss&apos;&gt;</span><br><span class="line">#map&#123;</span><br><span class="line">  width: 400px;</span><br><span class="line">  height: 250px;</span><br><span class="line">  margin:100px auto;</span><br><span class="line">  border:solid 1px red;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<h4 id="第二个实例-加载WMS（web-map-service）图片"><a href="#第二个实例-加载WMS（web-map-service）图片" class="headerlink" title="第二个实例,加载WMS（web map service）图片"></a>第二个实例,加载WMS（web map service）图片</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">const map = new Map(&#123;</span><br><span class="line">           target: &apos;map&apos;,</span><br><span class="line">           layers:[</span><br><span class="line">               new TileLayer(&#123;</span><br><span class="line">                   source: new OSM()</span><br><span class="line">               &#125;),</span><br><span class="line">               new ImageLayer(&#123;</span><br><span class="line">                   extent: [-13884991, 2870341, -7455066, 6338219],//设置地图的边界，超出范围则不渲染</span><br><span class="line">                   source: new ImageWMS(&#123;</span><br><span class="line">                       url: &apos;https://ahocevar.com/geoserver/wms&apos;,</span><br><span class="line">                       params: &#123;&apos;LAYERS&apos;: &apos;topp:states&apos;&#125;,</span><br><span class="line">                       ratio: 1,// 比例，值大于一，</span><br><span class="line">                       serverType: &apos;geoserver&apos;// WMS服务类型</span><br><span class="line">                   &#125;)</span><br><span class="line">               &#125;)</span><br><span class="line">           ],</span><br><span class="line">           view: new View(&#123;</span><br><span class="line">                center: [-6655.5402445057125, 6709968.258934638],</span><br><span class="line">                zoom: 13</span><br><span class="line">           &#125;)</span><br><span class="line">       &#125;)</span><br></pre></td></tr></table></figure>
<h4 id="第三个实例：地图view的使用"><a href="#第三个实例：地图view的使用" class="headerlink" title="第三个实例：地图view的使用"></a>第三个实例：地图view的使用</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">	&lt;div&gt;</span><br><span class="line">		&lt;div id=<span class="string">'map'</span>&gt;<span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">		&lt;input type=<span class="string">"button"</span> @click=<span class="string">'moveToLeft()'</span> value=<span class="string">'左移'</span>&gt; </span><br><span class="line">		&lt;input type=<span class="string">"button"</span> @click=<span class="string">'moveToTop()'</span> value=<span class="string">'上移'</span>&gt;</span><br><span class="line">		&lt;input type=<span class="string">"button"</span> @click=<span class="string">'moveToMax()'</span> value=<span class="string">'放大'</span>&gt;</span><br><span class="line">		&lt;input type=<span class="string">"button"</span> @click=<span class="string">'moveToBJ()'</span> value=<span class="string">'移动到合肥'</span>&gt; </span><br><span class="line">		&lt;input type=<span class="string">"button"</span> @click=<span class="string">'fitToChengdu()'</span> value=<span class="string">'移动到成都并自适应显示'</span>&gt; </span><br><span class="line">	&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">&lt;/</span>template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> <span class="string">'ol/ol.css'</span>; </span><br><span class="line"><span class="keyword">import</span> &#123;<span class="built_in">Map</span>, View&#125; <span class="keyword">from</span> <span class="string">'ol'</span>; </span><br><span class="line"><span class="keyword">import</span> &#123;Tile&#125; <span class="keyword">from</span> <span class="string">'ol/layer'</span>; </span><br><span class="line"><span class="keyword">import</span> &#123;transform&#125; <span class="keyword">from</span> <span class="string">'ol/proj'</span></span><br><span class="line"><span class="keyword">import</span> OSM <span class="keyword">from</span> <span class="string">'ol/source/OSM'</span>;  </span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span>&#123;</span><br><span class="line">	name:<span class="string">'sample3'</span>,</span><br><span class="line">	data () &#123;</span><br><span class="line">		<span class="keyword">return</span> &#123;<span class="attr">map</span>:&#123;&#125;&#125;</span><br><span class="line">	&#125;,</span><br><span class="line">	methods:&#123;</span><br><span class="line">		moveToLeft()&#123;</span><br><span class="line">			<span class="keyword">let</span> view = <span class="keyword">this</span>.map.getView(); </span><br><span class="line">			<span class="keyword">let</span> mapCenter = view.getCenter();</span><br><span class="line">      <span class="comment">// 让地图中心的x值增加，即可使得地图向左移动，增加的值根据效果可自由设定</span></span><br><span class="line">      mapCenter[<span class="number">0</span>] += <span class="number">50000</span>;</span><br><span class="line">      view.setCenter(mapCenter);</span><br><span class="line">      <span class="keyword">this</span>.map.render();</span><br><span class="line">		&#125;,</span><br><span class="line">		moveToTop()&#123;</span><br><span class="line">			<span class="keyword">let</span> view = <span class="keyword">this</span>.map.getView();</span><br><span class="line">			<span class="keyword">let</span> mapCenter=view.getCenter();</span><br><span class="line">			mapCenter[<span class="number">1</span>]+=<span class="number">50000</span>;</span><br><span class="line">			view.setCenter(mapCenter);</span><br><span class="line">			<span class="keyword">this</span>.map.render();</span><br><span class="line">		&#125;,</span><br><span class="line">		moveToMax()&#123;</span><br><span class="line">			<span class="keyword">let</span> view = <span class="keyword">this</span>.map.getView();</span><br><span class="line">			view.setZoom(view.getZoom()+<span class="number">1</span>);</span><br><span class="line">		&#125;,</span><br><span class="line">		moveToBJ()&#123;</span><br><span class="line">			<span class="keyword">let</span> view = <span class="keyword">this</span>.map.getView();</span><br><span class="line">			<span class="keyword">let</span> viewCenter=transform([<span class="number">117.215761</span>,<span class="number">31.801256</span>],<span class="string">'EPSG:4326'</span>,<span class="string">'EPSG:3857'</span>);</span><br><span class="line">			view.setCenter(viewCenter);</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="comment">//定位到成都并最大化显示</span></span><br><span class="line">		fitToChengdu()&#123;</span><br><span class="line">			<span class="comment">//fit(extent,size),extent是要显示的经纬度范围，size为在显示区域的长宽数组</span></span><br><span class="line">			<span class="keyword">this</span>.map.getView().fit([<span class="number">104</span>, <span class="number">30.6</span>, <span class="number">104.12</span>, <span class="number">30.74</span>], <span class="keyword">this</span>.map.getSize());</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;,</span><br><span class="line">	mounted () &#123; </span><br><span class="line">		  <span class="keyword">this</span>.map = <span class="keyword">new</span> <span class="built_in">Map</span>(&#123;</span><br><span class="line">			layers:[</span><br><span class="line">			<span class="keyword">new</span> Tile(&#123;<span class="attr">source</span>: <span class="keyword">new</span> OSM()&#125;)</span><br><span class="line">			],</span><br><span class="line">			view: <span class="keyword">new</span> View(&#123;</span><br><span class="line">				<span class="comment">// 设置北京为地图中心，此处进行坐标转换， 把EPSG:4326的坐标，转换为EPSG:3857坐标，因为ol默认使用的是EPSG:3857坐标</span></span><br><span class="line">				center: transform([<span class="number">116.41</span>, <span class="number">39.90</span>], <span class="string">'EPSG:4326'</span>, <span class="string">'EPSG:3857'</span>),</span><br><span class="line">				projection:<span class="string">'EPSG:3857'</span>,</span><br><span class="line">				zoom: <span class="number">10</span>,</span><br><span class="line">				<span class="comment">// 限制地图缩放最大级别为14，最小级别为10</span></span><br><span class="line">        minZoom: <span class="number">10</span>,</span><br><span class="line">        maxZoom: <span class="number">14</span></span><br><span class="line">			&#125;),</span><br><span class="line">			target: <span class="string">'map'</span></span><br><span class="line">		&#125;) </span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br><span class="line"><span class="regexp">&lt;style lang='scss' scoped&gt;</span></span><br><span class="line"><span class="regexp">#map&#123;</span></span><br><span class="line"><span class="regexp">	width:500px;</span></span><br><span class="line"><span class="regexp">	height: 250px;</span></span><br><span class="line"><span class="regexp">	border:solid 1px red;</span></span><br><span class="line"><span class="regexp">	margin: 50px auto;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp">&lt;/</span>style&gt;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本文章仅限于新手使用，所使用的实例是openlayer简单的是个实例。不过管中窥豹，可见一斑。openlayer多为配置项，如同echarts插件，说简单很简单，说复杂也有很多东西可以去学习。</p>
<p>  注：<br><a href="https://github.com/sean1224/gis-lixi" target="_blank" rel="noopener">项目已上传，欢迎star</a><br><a href="https://openlayers.org/" title="openlayer官网" target="_blank" rel="noopener">openlayer官网</a><br><a href="http://weilin.me/ol3-primer/index.html" title="openlayer中文翻译文档" target="_blank" rel="noopener">openlayer中文翻译文档</a><br><a href="https://cn.vuejs.org/v2/guide/" title="vue官网" target="_blank" rel="noopener">vue官网</a></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/js/" rel="tag"># js</a>
          
            <a href="/tags/openlayer/" rel="tag"># openlayer</a>
          
            <a href="/tags/gis/" rel="tag"># gis</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/05/12/js性能优化/" rel="next" title="JS性能优化">
                <i class="fa fa-chevron-left"></i> JS性能优化
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/05/25/canvas绘制水网管道图/" rel="prev" title="Vue项目canvas绘制水网管道图">
                Vue项目canvas绘制水网管道图 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.png" alt="li jian">
            
              <p class="site-author-name" itemprop="name">li jian</p>
              <div class="site-description motion-element" itemprop="description">出走半生，归来仍是少年</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue项目搭建"><span class="nav-number">2.</span> <span class="nav-text">vue项目搭建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#openlayer的简单实例"><span class="nav-number">3.</span> <span class="nav-text">openlayer的简单实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#第一个实例"><span class="nav-number">3.0.1.</span> <span class="nav-text">第一个实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第二个实例-加载WMS（web-map-service）图片"><span class="nav-number">3.0.2.</span> <span class="nav-text">第二个实例,加载WMS（web map service）图片</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#第三个实例：地图view的使用"><span class="nav-number">3.0.3.</span> <span class="nav-text">第三个实例：地图view的使用</span></a></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">
  
  &copy;
  
  
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">li jian</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.1"></script>

  <script src="/js/motion.js?v=7.1.1"></script>



  
  


  <script src="/js/affix.js?v=7.1.1"></script>

  <script src="/js/schemes/pisces.js?v=7.1.1"></script>



  
  <script src="/js/scrollspy.js?v=7.1.1"></script>
<script src="/js/post-details.js?v=7.1.1"></script>



  


  <script src="/js/next-boot.js?v=7.1.1"></script>


  

  

  

  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: '1knHEkWhq6iHvNiswIIOHD9t-gzGzoHsz',
    appKey: 'wVGfMbCux8bx8W61eQVjLYzy',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false,
    lang: '' || 'zh-cn'
  });
</script>




  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
